﻿#include "YunXiuIconFont.h"
#include <QtGui/QFontDatabase>
#include <QtCore/QDebug>
#include <QtCore/QRect>
#include <QtGui/QPainter>

YunXiuIconFont::YunXiuIconFont(QObject *parent)
	: QObject(parent)
{
    //因原mcp图标需要移除，故路径直接替换为云修的ttf，不做两个字体图标都add进来
    int fontId = QFontDatabase::addApplicationFont(":/IconFontV2/Resource/IconFontV2/yunxiufontV2.ttf");
	QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId);
	m_strFontFamily = fontFamilies.at(0);

	m_pFont = new QFont(m_strFontFamily);
}

YunXiuIconFont::~YunXiuIconFont()
{
    delete m_pFont;
}

YunXiuIconFont* YunXiuIconFont::instance()
{
	static YunXiuIconFont singleton;
	return &singleton;
}

QChar YunXiuIconFont::sample()
{
	return QChar(0xe616);
}

QString YunXiuIconFont::fontFamily() const
{
	return m_strFontFamily;
}

void YunXiuIconFont::paintIconFont(QPainter *painter, const QRect &rt,int fontSize,const QColor &color, YunXiuIconFont::IconIndex index) {
    if(nullptr == painter || !rt.isValid()){
        return;
    }
    painter->save();
    painter->setRenderHints(QPainter::TextAntialiasing | QPainter::Antialiasing);
    m_pFont->setPointSize(fontSize);
    painter->setFont(*m_pFont);
    painter->setPen(QPen(QBrush(color),painter->pen().widthF()));
    painter->drawText(rt,QChar(index),QTextOption(Qt::AlignCenter));

    painter->restore();
}

QPixmap YunXiuIconFont::renderToPixmap(YunXiuIconFont::IconIndex index, QSize size, int fontPixel, const QColor& foregroundClr) {
    if(size.isEmpty()) {
        return QPixmap();
    }
    QPixmap pix(size);
    pix.fill(QColor(Qt::transparent));
    QPainter painter(&pix);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
    QFont font;
    font.setFamily(fontFamily());
    font.setPixelSize(fontPixel);
    painter.setFont(font);
    painter.setPen(foregroundClr);
    painter.drawText(QRect(QPoint(0, 0), size), QString(QChar(index)), QTextOption(Qt::AlignmentFlag::AlignCenter));
    painter.end();
    return pix;
}
